<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加或者修改讲师</title>
<link rel="stylesheet" href="/layui/css/layui.css"></link>
<style type="text/css">

	table tr td{
		padding: 10px;
	}

</style>
</head>
<body>
<div style="padding: 20px">
	<form method="post" >
		<table>
			<tr>
				<td>讲师名称：</td>
				<td><input type="text" id="nickName" name="nickName" class="layui-input" style="width: 300px"/></td>
			</tr>
			<tr>
				<td>讲师描述：</td>
				<td>
					<textarea type="text" id="remark" name="remark" class="layui-input" style="width: 300px;height: 100px" ></textarea>
				</td>
			</tr>
			<tr>
				<td><button class="layui-btn" onclick="submitData();return false;">提交</button></td>
				<td><font id="errorInfo" color="red"></font></td>
			</tr>
		</table>
	</form>
</div>
<script src="/layui/layui.js"></script>
<script src="/js/jquery.js"></script>
<script src="/js/common.js"></script>
<script type="text/javascript">

    layui.use(['form'], function(){
        var form = layui.form;

    });

	function submitData(){
		var nickName=$("#nickName").val().trim();
		var remark=$("#remark").val().trim();
		if(nickName=="") {
	　　　　    $("#errorInfo").text("请输入讲师名称！");
			  $("nickName").focus();
	　　　　　　      return false; 
	　　　} 
	   var id=getQueryVariable("id");
	   if(id){
		   $.post("/admin/teacher/save",{id:id,nickName:nickName,remark:remark},function(result){
			  if(result.success){
                  layer.alert('修改成功！',function () {
                      parent.reloadPage();
                  });
			  }else{
				  $("#errorInfo").text(result.errorInfo);
			  }
		  },"json");
	   }else{
		   $.post("/admin/teacher/save",{nickName:nickName,remark:remark,imageName:'default.jpg'},function(result){
				  if(result.success){
                      layer.alert('添加成功！',function () {
                          parent.reloadPage();
                      });
				  }else{
					  $("#errorInfo").text(result.errorInfo);
				  }
			  },"json");
	   }
	}
	
	
	
	function getQueryVariable(variable){
	       var query = window.location.search.substring(1);
	       var vars = query.split("&");
	       for (var i=0;i<vars.length;i++) {
	               var pair = vars[i].split("=");
	               if(pair[0] == variable){return pair[1];}
	       }
	       return(false);
	}
	
	$(function(){ 
	　　
		var id=getQueryVariable("id");
		
		if(id){
			$.post("/admin/teacher/findById",{id:id},function(result){
				  if(result.success){
					  var teacher=result.teacher;
					  $("#nickName").val(teacher.nickName);
					  $("#remark").val(teacher.remark);
				  }else{
                      layer.alert('服务器加载有问题，请联系管理员！');
				  }
			  },"json");
		}
	}); 
	

</script>
</body>
</html>